<?php

class Admin_ProjectController extends Zend_Controller_Action
{	
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    
    public function indexAction(){           
        $modelProject              = new Admin_Model_Project();                
        $projectList               = $modelProject->getAllData1()->toArray();
        $this->view->projectList   = $projectList;                
    }
    
    public function synchronizeAction(){
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $modelProject = new Admin_Model_Project();
        $upload = new Zend_File_Transfer();
        $files = $upload->getFileInfo();       
        if($upload->getFileName(null,false)){
            $upload->setDestination(PUBLIC_PATH.'/upload/file/');            
            $upload->receive();
        }        
        $datas = My_Pear::getInstance_ExcelReader(PUBLIC_PATH.'/upload/file/'.$upload->getFileName(null,false));        
        $modelDistrict = new Admin_Model_District();        
        $districtListTemp=$modelDistrict->getAllData();        
        foreach($districtListTemp as $district){
            $districtList[]=$district['districtid'];
        }                       
        foreach($datas as $data){
            if(in_array($data['districtId'],$districtList)){
                try{            
                   $modelProject->addData($data);                    
                }catch(Zend_Db_Exception $e){            
                    $modelProject->updateData($data['projectid'],$data);                        
                }                                    
            }                                                                                        
        }                                         
        unlink(PUBLIC_PATH.'/upload/file/'.$upload->getFileName(null,false));           
        $this->_helper->redirector('index', 'project', 'admin');                         
    }
    
    public function exportAction(){        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $modelPrject = new Admin_Model_Project();
                
        $workbook = My_Pear::getInstance_ExcelWriter();
        
        // sending HTTP headers
        $workbook->send($modelPrject->info(Zend_Db_Table_Abstract::NAME).'.xls');        
        $workbook->setVersion(8, 'utf-8');
                                   
        $projectList = $modelPrject->getAllData1();                
        $cols=$modelPrject->info(Zend_Db_Table_Abstract::COLS);

        // Creating a worksheet
        $worksheet =& $workbook->addWorksheet($modelPrject->info(Zend_Db_Table_Abstract::NAME));
        $worksheet->setInputEncoding('utf-8');
        $content = &$workbook->addFormat(); 		                
        
        // The actual data
        $i=0;
        foreach($cols as $col){
            $worksheet->write(0, $i++, $col);
        }               	
        $i=1;                
        foreach($projectList as $pro){
            $worksheet->write($i, 0, $pro['projectId']);
            $worksheet->write($i, 1, $pro['projectName']);
            $worksheet->writeString($i++, 2, $pro['districtId']);
        }               
        
        // Let's send the file
        $workbook->close();        
    }
    public function addAction(){   
        $form = new Admin_Form_Project();        
        $form->submit->setLabel('Thêm mới');                
        $this->view->form = $form;        
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $addData['projectId']   = $form->getValue('projectId');
                $addData['projectName'] = $form->getValue('projectName');
                $addData['districtId']  = $form->getValue('districtId');                                                                
                
                $modelProject=new Admin_Model_Project();                                   
                $proId = $modelProject->addData($addData);
                if($proId){
                    $this->_helper->redirector('index', 'project', 'admin');
                }else{
                    $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                    $this->view->flag = "error";
                    $form->populate($formData);
                }                
            }else{                 
                $form->populate($formData);                
            }
        }
    }
    public function editAction(){
        $proId = (int)($this->_getParam('projectId', 0));        
        $modelProject = new Admin_Model_Project();
        $projectCheck = $modelProject->getDataByProjectId($proId);               
        $form = new Admin_Form_Project();
        $form->projectId->setAttrib('readonly','true');                             
        $form->submit->setLabel('Lưu thay đổi');
        $this->view->form = $form;        
        if($projectCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if($form->isValid($formData)){                    
                    $editData['projectName'] = $form->getValue('projectName');
                    $editData['districtId']  = $form->getValue('districtId');
                    
                    $modelProject=new Admin_Model_Project();                                      
                    $modelProject->updateData($proId,$editData);
                    
                    $this->_helper->redirector('index', 'project', 'admin');
                }else{                    
                    $form->populate($formData);
                }
            }else{                    
                $form->populate($projectCheck->toArray());
            }
        }else{
            $this->_helper->redirector('index', 'project', 'admin');
        }
    }
    public function deleteAction(){
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $proId = $this->getRequest()->getPost('projectId');
                $modelProject = new Admin_Model_Project();
                $modelProject->deleteData($proId);
                // ghi log
            }
            $this->_helper->redirector('index', 'project', 'admin');
        }else{
            $id = $this->_getParam('projectId', 0);
            $modelProject = new Admin_Model_Project();
            $this->view->projectCheck = $modelProject->getDataByProjectId($id);
        }
    }
    public function getprojectAction(){        
        $district = $this->_request->getParam('district');                            
        $models=new Admin_Model_Project();
        $projListT=$models->getDataByDistrictId($district)->toArray();        
        $projList = array_column($projListT, 'projectName', 'projectId');
                                
        echo json_encode($projList,JSON_UNESCAPED_UNICODE);        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);        
    }
}